<?php
class M_student_payment_record Extends MY_Model
{
	
		protected $_table = 'student_payment_records';
		protected $enrollments_table = 'enrollments';
		protected $last_input_id;
		
		public function __construct()
		{
			parent::__construct();
		}
		
		public function get_payment_record_by_enrollment_id($id)
		{	
			$sql = "SELECT `spr_id`,
							`spr_or_no`,
							`spr_payment_date`,
							`spr_ammt_paid`,
              `spr_mode_of_payment`,
              `spr_remarks`,
							`spr_created`
					FROM `student_payment_records` 
					WHERE `spr_enrollment_id` = ? 
					AND spr_is_deleted = 0
					ORDER BY spr_payment_date ASC";
			$query = $this->db->query($sql,array($id));
			return $query->num_rows() >=1 ? $query->result() : FALSE;
		}
		
		public function current_payment_record_id($e_id)
		{
			$sql = "SELECT max(spr_id) as current_spr_id 
					FROM student_payment_records 
					WHERE spr_enrollment_id = ?
					AND spr_is_deleted = 0
					";
			$query = $this->db->query($sql,array($e_id));
			return $query->num_rows() > 0 ? $query->row()->current_spr_id : FALSE;
		}
	
		public function get_payment_breakdown_by_spr_id($id)
	{
		if(is_numeric($id))
		{
			$id = intval($id);
			
			$sql = "SELECT `spb`.`spb_id`,
							`spb`.`spb_fee_name`,
							`spb`.`spb_fee_rate`,
							`spb`.`spb_amount_paid`,
							`spb`.`spb_remaining_balance`,
							`spb`.`spb_is_tuition_fee`,
							`spb`.`spb_is_other_fee`,
							`spb`.`spb_is_elective_fee`,
							`spb`.`spb_is_cca_fee`,
							`spb`.`spb_is_misc_fee`,
							`spr`.`spr_or_no`
					FROM student_payment_breakdown spb
					LEFT JOIN student_payment_records spr ON spb.spb_spr_id = spr.spr_id
					WHERE spb_spr_id = ? 
					ORDER BY spb_is_tuition_fee,
					spb_is_misc_fee ASC";
			$query = $this->db->query($sql,array($id));
			return $query->num_rows() >=1 ? $query->result() : FALSE;
		}else{
			return FALSE;
		}
	}
}